Imports System.Collections.Generic
Namespace BoAddonBiz_Authorisation
    Public Class FormAuthorisation
        Public Shared List As New Dictionary(Of String, FormAuthorisation)
        Private _FormType As String
        Private _FormAuth As auAuthorisationType
        Private _AuthNoneItem As String()
        Private _AuthReadItem As String()
        Private _AuthALLItem As String()

        Sub New(ByVal rFormType As String)
            _FormType = rFormType
            GetFormAuthorisations()
            GetItemAuthorisations()
        End Sub

        Public Shared Function getFormAuthorisation(ByVal FormType As String) As FormAuthorisation
            If List.ContainsKey(FormType) Then
                Return List(FormType)
            Else
                List.Add(FormType, New FormAuthorisation(FormType))
                Return List(FormType)
            End If
        End Function

        Private Sub GetFormAuthorisations()
            Dim oRs As SAPbobsCOM.Recordset
            Dim UserSign As String
            Dim sql_GetFormAuthorisation As String = "SELECT T0.[U_AuthValue] FROM [dbo].[@AVA_AU_AUTHORISE]  T0 WHERE T0.[DocEntry] = %UserSign and T0.[U_Target] = '%Target' and T0.[U_Type] = '3-F'"
            Try
                If B1Console_Authoristation.CanUse Then
                    oRs = B1Addon.B1Addon.SBOCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    UserSign = B1Addon.B1Addon.SBOCompany.UserSignature
                    oRs.DoQuery(sql_GetFormAuthorisation.Replace("%UserSign", UserSign).Replace("%Target", _FormType))
                    If oRs.RecordCount > 0 Then
                        If oRs.Fields.Item(0).Value = "A" Then
                            Me._FormAuth = auAuthorisationType.ALL
                        ElseIf oRs.Fields.Item(0).Value = "R" Then
                            Me._FormAuth = auAuthorisationType.READ
                        ElseIf oRs.Fields.Item(0).Value = "N" Then
                            Me._FormAuth = auAuthorisationType.NONE
                        End If
                    Else
                        Me._FormAuth = auAuthorisationType.ALL
                    End If
                Else
                    Me._FormAuth = auAuthorisationType.ALL
                End If
            Catch ex As Exception
                Throw New Exception(ex.Message & "by Apply_Authorisations.")
            End Try
        End Sub

        Private Sub GetItemAuthorisations()
            Dim oRs As SAPbobsCOM.Recordset
            Dim UserSign As String
            Dim AryALL As New List(Of String)
            Dim AryNone As New List(Of String)
            Dim AryRead As New List(Of String)
            Dim sql_GetItemAuthorisation As String = "SELECT T0.[U_SubTarget], T0.[U_AuthValue] FROM [dbo].[@AVA_AU_AUTHORISE]  T0 WHERE T0.[DocEntry] = %UserSign and T0.[U_Target] = '%Target' and T0.[U_Type] = '4-I'"
            Try
                If B1Console_Authoristation.CanUse Then
                    UserSign = B1Addon.B1Addon.SBOCompany.UserSignature
                    oRs = B1Addon.B1Addon.SBOCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    oRs.DoQuery(sql_GetItemAuthorisation.Replace("%Target", _FormType).Replace("%UserSign", UserSign))
                    If oRs.RecordCount > 0 Then
                        Do Until oRs.EoF
                            If oRs.Fields.Item(1).Value = "N" Then
                                AryNone.Add(oRs.Fields.Item(0).Value.ToString)
                            ElseIf oRs.Fields.Item(1).Value = "R" Then
                                AryRead.Add(oRs.Fields.Item(0).Value.ToString)
                            ElseIf oRs.Fields.Item(1).Value = "A" Then
                                AryALL.Add(oRs.Fields.Item(0).Value.ToString)
                            End If
                            oRs.MoveNext()
                        Loop
                    End If
                    Me._AuthALLItem = AryALL.ToArray
                    Me._AuthNoneItem = AryNone.ToArray
                    Me._AuthReadItem = AryRead.ToArray
                End If

            Catch ex As Exception
                Throw New Exception(ex.Message & "by Apply_Authorisations.")
            End Try
        End Sub

        Public ReadOnly Property FormType As String
            Get
                Return _FormType
            End Get
        End Property

        Public ReadOnly Property FormAuthorisationType() As SAPBoAddon.BoAddonBiz_Authorisation.auAuthorisationType
            Get
                Return Me._FormAuth
            End Get
        End Property

        Public ReadOnly Property ItemAuthorisation_None() As String()
            Get
                Return Me._AuthNoneItem
            End Get
        End Property

        Public ReadOnly Property ItemAuthorisation_ALL() As String()
            Get
                Return Me._AuthALLItem
            End Get
        End Property

        Public ReadOnly Property ItemAuthorisation_ReadOnly() As String()
            Get
                Return Me._AuthReadItem
            End Get
        End Property

    End Class

    Public Enum auAuthorisationType
        ALL = 0
        READ = 1
        NONE = 2
    End Enum
End Namespace

